package com.usefullc.user.service;

import com.usefullc.system.query.BaseQuery;
import com.usefullc.system.service.AbstractBizService;
import com.usefullc.user.dao.RoleDao;
import com.usefullc.user.domain.AuthRes;
import com.usefullc.user.domain.Role;
import com.usefullc.user.param.RoleAuthResParam;
import com.usefullc.user.query.RoleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Sampson
 * @date: 2022-01-10 14:10
 */
@Service
public class RoleService extends AbstractBizService<Role,RoleQuery> {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Predicate> getPredicates(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder, RoleQuery thisQuery) {
        List<Predicate> predicates = new ArrayList<>();
        return predicates;
    }


    @Transactional
    public void saveRoleAuthRes(RoleAuthResParam param){
        //删除旧的
//        roleDao.deleteRoleAuthResByRoleId(role.getId());
        List<Long> authResIds = param.getAuthResIds();
        Long roleId = param.getRoleId();
        Role role = this.findById(roleId);
        List<AuthRes> authResList = new ArrayList<>();
        role.setAuthResList(authResList);
        for (Long authResId : authResIds) {
            AuthRes authRes = new AuthRes();
            authRes.setId(authResId);
            authResList.add(authRes);
        }
        this.save(role);
    }
}
